<?php

/**
 *	Class DoctorsSpecialities
 *  --------------
 *	Description : encapsulates methods and properties
 *	Written by  : ApPHP
 *  Updated	    : 05.02.2011
 *  Usage       : Medical Appointment
 *
 *	PUBLIC				  	STATIC				 		PRIVATE
 * 	------------------	  	---------------     		---------------
 *	__construct             GetDoctorsBySpeciality *	
 *	__destruct              GetDoctorsByName
 *	
 *  1.0.1
 *      - added GetDoctorsBySpeciality
 *      - 
 *      -
 *      -
 *      -
 *	
 **/


class DoctorsSpecialities extends MicroGrid {
	
	protected $debug = false;
	
	//==========================================================================
    // Class Constructor
	//		@param $doctor_id
	//==========================================================================
	function __construct($doctor_id = 0)
	{		
		parent::__construct();
		
		$this->params = array();
		
		## for standard fields
		if(isset($_POST['speciality_id'])) $this->params['speciality_id'] = prepare_input($_POST['speciality_id']);
		if(isset($_POST['doctor_id'])) $this->params['doctor_id'] = prepare_input($_POST['doctor_id']);
		
		## for checkboxes 
		//$this->params['is_active'] = isset($_POST['is_active']) ? prepare_input($_POST['is_active']) : '0';

		## for images (not necessary)
		//if(isset($_POST['icon'])){
		//	$this->params['icon'] = prepare_input($_POST['icon']);
		//}else if(isset($_FILES['icon']['name']) && $_FILES['icon']['name'] != ''){
		//	// nothing 			
		//}else if (self::GetParameter('action') == 'create'){
		//	$this->params['icon'] = '';
		//}

		## for files:
		// define nothing

		//$this->params['language_id'] = MicroGrid::GetParameter('language_id');
	
		//$this->uPrefix 		= 'prefix_';
		
		$this->primaryKey 	= 'id';
		$this->tableName 	= TABLE_DOCTORS_SPECIALITIES;
		$this->dataSet 		= array();
		$this->error 		= '';
		$this->formActionURL = 'index.php?admin=doctors_specialities&doctor_id='.(int)$doctor_id;
		$this->actions      = array('add'=>true, 'edit'=>(($doctor_id > 1) ? true : false), 'details'=>true, 'delete'=>true);
		$this->actionIcons  = true;
		$this->allowRefresh = true;
		$this->allowTopButtons = false;
		$this->alertOnDelete = ''; // leave empty to use default alerts

		$this->allowLanguages = false;
		$this->languageId  	= ''; //($this->params['language_id'] != '') ? $this->params['language_id'] : Languages::GetDefaultLang();
		$this->WHERE_CLAUSE = ''; // WHERE .... / 'WHERE language_id = ''.$this->languageId.''';				
		$this->ORDER_CLAUSE = ''; // ORDER BY '.$this->tableName.'.date_created DESC
		
		$this->isAlterColorsAllowed = true;

		$this->isPagingAllowed = true;
		$this->pageSize = 20;

		$this->isSortingAllowed = true;

		$this->isExportingAllowed = false;
		$this->arrExportingTypes = array('csv'=>false);
		
		$this->isFilteringAllowed = false;
		// define filtering fields
		$this->arrFilteringFields = array(
			// 'Caption_1'  => array('table'=>'', 'field'=>'', 'type'=>'text', 'sign'=>'=|like%|%like|%like%', 'width'=>'80px', 'visible'=>true),
			// 'Caption_2'  => array('table'=>'', 'field'=>'', 'type'=>'dropdownlist', 'source'=>array(), 'sign'=>'=|like%|%like|%like%', 'width'=>'130px', 'visible'=>true),
		);

		///$date_format = get_date_format('view');
		///$date_format_edit = get_date_format('edit');				
		///$currency_format = get_currency_format();

		// prepare specialities array
		$total_specialities = Specialities::GetAllActive('id NOT IN (SELECT speciality_id FROM '.$this->tableName.' WHERE doctor_id = '.(int)$doctor_id.')');
		$arr_specialities = array();
		foreach($total_specialities[0] as $key => $val){
		 	$arr_specialities[$val['id']] = $val['name'];
		}

		//---------------------------------------------------------------------- 
		// VIEW MODE
		// format: strip_tags
		// format: nl2br
		// format: 'format'=>'date', 'format_parameter'=>'M d, Y, g:i A''
		// format: 'format'=>'currency', 'format_parameter'=>'european|2' or 'format_parameter'=>'american|4'
		//---------------------------------------------------------------------- 
		$this->VIEW_MODE_SQL = 'SELECT
									ds.'.$this->primaryKey.',
									s.name,
									s.description
								FROM '.$this->tableName.' ds
									INNER JOIN '.TABLE_DOCTORS.' d ON ds.doctor_id = d.id
									INNER JOIN '.TABLE_SPECIALITIES.' s ON ds.speciality_id = s.id
								WHERE
									ds.doctor_id = '.$doctor_id;		
		// define view mode fields
		$this->arrViewModeFields = array(
			'name'          => array('title'=>_NAME, 'type'=>'label', 'align'=>'left', 'width'=>'250px', 'sortable'=>true, 'nowrap'=>'', 'visible'=>'', 'tooltip'=>'', 'maxlength'=>'40', 'format'=>'', 'format_parameter'=>''),
			'description'   => array('title'=>_DESCRIPTION, 'type'=>'label', 'align'=>'left', 'width'=>'', 'sortable'=>true, 'nowrap'=>'', 'visible'=>'', 'tooltip'=>'', 'maxlength'=>'100', 'format'=>'', 'format_parameter'=>''),
		);
		
		//---------------------------------------------------------------------- 
		// ADD MODE
		// - Validation Type: alpha|numeric|float|alpha_numeric|text|email|ip_address|password|date
		// 	 Validation Sub-Type: positive (for numeric and float)
		//   Ex.: 'validation_type'=>'numeric', 'validation_type'=>'numeric|positive'
		// - Validation Max Length: 12, 255... Ex.: 'validation_maxlength'=>'255'
		// - Validation Min Length: 4, 6... Ex.: 'validation_minlength'=>'4'
		// - Validation Max Value: 12, 255... Ex.: 'validation_maximum'=>'99.99'
		//---------------------------------------------------------------------- 
		// define add mode fields
		$this->arrAddModeFields = array(
			'speciality_id' => array('title'=>_SPECIALITY, 'type'=>'enum', 'width'=>'', 'required'=>true, 'readonly'=>false, 'default'=>'', 'source'=>$arr_specialities, 'default_option'=>'', 'unique'=>false, 'javascript_event'=>''),			
			'doctor_id'     => array('title'=>'', 'type'=>'hidden', 'required'=>true, 'readonly'=>false, 'default'=>$doctor_id),										
		);

		//---------------------------------------------------------------------- 
		// EDIT MODE
		// - Validation Type: alpha|numeric|float|alpha_numeric|text|email|ip_address|password|date
		//   Validation Sub-Type: positive (for numeric and float)
		//   Ex.: 'validation_type'=>'numeric', 'validation_type'=>'numeric|positive'
		// - Validation Max Length: 12, 255... Ex.: 'validation_maxlength'=>'255'
		// - Validation Min Length: 4, 6... Ex.: 'validation_minlength'=>'4'
		// - Validation Max Value: 12, 255... Ex.: 'validation_maximum'=>'99.99'
		//---------------------------------------------------------------------- 
		$this->EDIT_MODE_SQL = 'SELECT
								ds.'.$this->primaryKey.',
								ds.doctor_id,
								ds.speciality_id,
								s.name as speciality_name,
								s.description as speciality_description
							FROM '.$this->tableName.' ds
								INNER JOIN '.TABLE_SPECIALITIES.' s ON ds.speciality_id = s.id
							WHERE ds.'.$this->primaryKey.' = _RID_';		
		// define edit mode fields
		$this->arrEditModeFields = array(
			'speciality_name' => array('title'=>_SPECIALITY, 'type'=>'label'),
			'speciality_id'   => array('title'=>_BUTTON_CHANGE, 'type'=>'enum', 'width'=>'', 'required'=>true, 'readonly'=>false, 'default'=>'', 'source'=>$arr_specialities, 'default_option'=>'', 'unique'=>false, 'javascript_event'=>''),			
			'doctor_id'       => array('title'=>'', 'type'=>'hidden', 'required'=>true, 'readonly'=>false, 'default'=>$doctor_id),										
		);

		//---------------------------------------------------------------------- 
		// DETAILS MODE
		//----------------------------------------------------------------------
		$this->DETAILS_MODE_SQL = $this->EDIT_MODE_SQL;
		$this->arrDetailsModeFields = array(
			'speciality_name'        => array('title'=>_SPECIALITY, 'type'=>'label'),
			'speciality_description' => array('title'=>_DESCRIPTION, 'type'=>'label'),
		);
	}
	
	//==========================================================================
    // Class Destructor
	//==========================================================================
    function __destruct()
	{
		// echo 'this object has been destroyed';
    }

	/**
	 *	Get doctors by seciality
	 *		@param $speciality_id
	 */
	public static function GetDoctorsBySpeciality($speciality_id = 0)
	{
		$sql = 'SELECT
					ds.id,
					d.id as doctor_id,
					d.first_name,
					d.middle_name,
					d.last_name,
					d.gender,
					d.title,
					d.medical_degree,
					d.doctor_photo,
					d.doctor_photo_thumb,
					s.name as speciality_name
				FROM '.TABLE_DOCTORS_SPECIALITIES.' ds
					INNER JOIN '.TABLE_DOCTORS.' d ON ds.doctor_id = d.id
					INNER JOIN '.TABLE_SPECIALITIES.' s ON ds.speciality_id = s.id
				WHERE
					d.is_active = 1 AND 
					ds.speciality_id = '.(int)$speciality_id;
		return database_query($sql, DATA_AND_ROWS);					
	}
	
	/**
	 *	Get doctors by name
	 *		@param $name
	 */
	public static function GetDoctorsByName($name = '')
	{
		$sql = 'SELECT
					ds.id,
					d.id as doctor_id,
					d.first_name,
					d.middle_name,
					d.last_name,
					d.gender,
					d.title,
					d.medical_degree,
					d.doctor_photo,
					d.doctor_photo_thumb,
					s.name as speciality_name
				FROM '.TABLE_DOCTORS_SPECIALITIES.' ds
					INNER JOIN '.TABLE_DOCTORS.' d ON ds.doctor_id = d.id
					INNER JOIN '.TABLE_SPECIALITIES.' s ON ds.speciality_id = s.id
				WHERE
					d.is_active = 1 AND 
					(d.last_name LIKE \'%'.$name.'%\' OR d.first_name LIKE \'%'.$name.'%\')';
		return database_query($sql, DATA_AND_ROWS);	
	}
	
}
?>